﻿//***********************************************************************
// Assembly         : IGolovkine.AoA.Tests
// Author           : Ilia
// Created          : 06-11-2011
//
// Last Modified By : Ilia
// Last Modified On : 01-04-2010
// Description      : 
//
// Copyright        : (c) Microsoft. All rights reserved.
//***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using IGolovkine.AoA.Tcp;
using System.Reactive.Linq;

namespace IGolovkine.AoA.Tests
{
    [TestClass]
    public class MessageQueueTests
    {
        [TestMethod]
        public void FairnessTest()
        {
        }

        [TestMethod]
        public void Dequeue()
        {
            //Observable.Context = new ApplicationSynchronizationContext();

            MessageAggregator queue = new MessageAggregator();
            Assert.AreEqual(0, queue.Dequeue(1).Length);
            Assert.AreEqual(0, queue.Dequeue(0).Length);

            queue.Input.OnNext(new TcpConnectionData(CorrelationId.Next(), new byte[2]));
            Assert.AreEqual(0, queue.Dequeue(0).Length);
            Assert.AreEqual(1, queue.Dequeue(1).Length);
            Assert.AreEqual(1, queue.Dequeue(1).Length);
            Assert.AreEqual(0, queue.Dequeue(1).Length);

            queue.Input.OnNext(new TcpConnectionData(CorrelationId.Next(), new byte[] { 1 }));
            queue.Input.OnNext(new TcpConnectionData(CorrelationId.Next(), new byte[] { 10, 11, 12 }));
            queue.Input.OnNext(new TcpConnectionData(CorrelationId.Next(), new byte[] { 20, 21, 22, 23 }));
            //Assert.AreEqual(8, queue.GetBufferedDataLength());

            Assert.AreEqual(0, queue.Dequeue(0).Length);
            Message[] messages = queue.Dequeue(2);
            Assert.AreEqual(2, messages.Length);

            // {}));
            // { 11, 12 }));
            // { 21, 22, 23 }));

            AssertEX.AreEqual(new byte[] { 1 }, ((TcpConnectionData)messages[0]).GetData());
            AssertEX.AreEqual(new byte[] { 10 }, ((TcpConnectionData)messages[1]).GetData());

            messages = queue.Dequeue(3);
            Assert.AreEqual(2, messages.Length);


            // {}));
            // {  11*, 12* }));
            // {  20*, 21, 22, 23 }));

            AssertEX.AreEqual(new byte[] { 11, 12 }, ((TcpConnectionData)messages[0]).GetData());
            AssertEX.AreEqual(new byte[] { 20 }, ((TcpConnectionData)messages[1]).GetData());
            //Assert.AreEqual(3, queue.GetBufferedDataLength());

            // {  21, 22, 23 }));
            messages = queue.Dequeue(5);
            Assert.AreEqual(1, messages.Length);
            AssertEX.AreEqual(new byte[] { 21, 22, 23 }, ((TcpConnectionData) messages[0]).GetData());
        
        }

        [TestMethod]
        public void Aggregation()
        {
            MessageAggregator queue = new MessageAggregator();
            TestPacket packet = new TestPacket(100);
            CorrelationId connectionId = CorrelationId.Next();

            queue.Input.OnNext(new TcpConnectionData(connectionId, packet.GetData()));
            queue.Input.OnNext(new TcpConnectionData(connectionId, packet.GetData()));
            queue.Input.OnNext(new TcpConnectionData(CorrelationId.Next(), packet.GetData()));
            queue.Input.OnNext(new TcpConnectionData(CorrelationId.Next(), packet.GetData()));
            queue.Input.OnNext(new TcpConnectionData(connectionId, packet.GetData()));

            Message[] message = queue.DequeueAll();
            Assert.AreEqual(3, message.Length);

            Assert.AreEqual(1, message.OfType<TcpConnectionData>().Where(m => m.ConnectionId == connectionId).Count());
            Assert.AreEqual(2, message.OfType<TcpConnectionData>().Where(m => m.ConnectionId != connectionId).Count());
        }
    }
}
